﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Universe.SqlTrace;

namespace Universe.Cache.Tests
{
    class MyEnvironment
    {
        public static readonly MyEnvironment Instance = new MyEnvironment();
        
        public string FileName;
        // public CacheSqlCeConnection CacheConfig;
        public ICacheStorage Storage;

        private MyEnvironment()
        {
            var s1 = Environment.ExpandEnvironmentVariables("%TEMP%");
            if (string.IsNullOrEmpty(s1))
                s1 = Environment.SystemDirectory.Substring(0, 2) + "\\Temp";

            string s2 = Path.Combine(s1, "Stress " + Guid.NewGuid());
            s2 = new FileInfo(s2).FullName;

            try
            {
                Directory.CreateDirectory(Path.GetDirectoryName(s2));
            }
            catch
            {
            }

            FileName = s2;

            var CacheConfig = new CacheSqlCeConnection(FileName + ".sdf");
            Storage = new CacheSqlCeStorage(CacheConfig);

/*
            var cs = new CachePerstConnection(FileName + ".cache");
            Storage = new CachePerstStorage(cs);
*/

        }

        public List<SnapshotItem> Snapshot = new List<SnapshotItem>();

        public void CreateStressDB(int count, int size)
        {
            PInvoke.DeleteFileOnReboot(FileName);

            Random rnd = new Random(1);
            var n = count;
            for (int i = 0; i < n; i++)
            {
                string key = "Key: " /* + new string('-', 11+i*100) */ + i;
                byte[] stamp = BitConverter.GetBytes(i);
                DateTime expired = DateTime.Now.AddDays(2);
                Snapshot.Add(new SnapshotItem() { Expired = expired, Key = key, Stamp = stamp });
            }

            for (int s = 1; s <= 1;s++)
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                for (int i = 0; i < n; i++)
                {
                    string key = Snapshot[i].Key;
                    byte[] stamp = Snapshot[i].Stamp;
                    int sz = size;
                    byte[] value = new byte[rnd.Next(sz, sz+1)];
                    DateTime expired = Snapshot[i].Expired;
                    Storage.Put(key, stamp, expired, value);

                    if (i % 100 == 0)
                    {
                        Console.Write("\rCreate DB {1}: {0}", i, s);
                        // Storage.SelectOnCleaning();
                    }
                }

                Console.WriteLine("\rCreate DB {2}: {0}. Total req/s: {1}", "Done",
                                  (n / sw.Elapsed.TotalSeconds).ToString("f1"),
                                  s);
            }
        }

        public class SnapshotItem
        {
            public string Key;
            public byte[] Stamp;
            public DateTime Expired;
        }
    }
}
